package io;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.CharUtils;
import org.junit.Test;

import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * @author Khai Loon
 * @version 2017/8/17
 */
public class FileTest {

    @Test
    public void get() throws IOException {
//        List<String> bytes = FileUtils.readLines(new File("/Users/lkl/Downloads/temp/1005010194_20191118_transfer_00002.txt"));
        String s = "￥ ）@（1|312312";
        System.out.println(Arrays.toString(s.toCharArray()));
    }

    @Test
    public void reader() throws IOException {
        File file = new File("/work/public/bold/early/src/main/java/io/reader.text");

        if (!file.exists()) return;
        String read;
        BufferedReader br = new BufferedReader(new FileReader(file));
        while ((read = br.readLine()) != null) {
            System.out.print(read);
        }
        br.close();
    }


    @Test
    public void write() throws IOException {
        File file = new File("/work/public/bold/early/src/main/java/io/write.text");

        if (!file.exists()) file.createNewFile();


        BufferedWriter bw = new BufferedWriter(new FileWriter(file));

        bw.write("写进来的内容");
        bw.flush();
        bw.close();
    }


    @Test
    public void copy() throws IOException {
        File file = new File("/work/public/bold/early/src/main/java/io/reader.text");

        StringBuffer sb = new StringBuffer();
        String read;

        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);

        while ((read = br.readLine()) != null) {
            sb.append(read);
        }

        File file2 = new File("/work/public/bold/early/src/main/java/io/write.text");

        BufferedWriter bw = new BufferedWriter(new FileWriter(file2));

        bw.write(sb.toString());
        bw.flush();
        bw.close();
    }

    @Test
    public void tryWith() {
        File file = new File("/work/public/bold/early/src/main/java/io/reader.text");
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            byte[] bytes = new byte[1024];
            fileInputStream.read(bytes);
            System.out.println(new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}